﻿using System;
using System.Linq;

namespace CRM.Database.Model
{
    public class Syncronizer
    {
        /// <summary>
        /// Синхронизация цветов
        /// </summary>
        /// <returns>Результат операции</returns>
        public bool ColorsSyncronise()
        {
            bool result = true;

            var colorsCollection = new ColorsCollection();

            foreach (SapMaterialExport item in DB.Entities.SapMaterialExport.ToList())
            {
                string SAPColorOutside = item.Farbe_Aussen;
                string SAPColorInside = item.Farbe_Innen;
                string SAPColorDicht = item.Dichtungsfarbe;

                if (!string.IsNullOrEmpty(SAPColorOutside))
                {
                    Colors OutsideColor = colorsCollection.GetColorBySAPId(SAPColorOutside);

                    if (OutsideColor == null)
                    {
                        var newColor = new Colors
                                           {
                                               IsActive = true,
                                               NameEn = SAPColorOutside,
                                               NameRu = SAPColorOutside,
                                               NameSAP = SAPColorOutside
                                           };

                        result = colorsCollection.Add(newColor, "28prygun01");

                        if (!result)
                            return false;
                    }
                }

                if (!string.IsNullOrEmpty(SAPColorInside))
                {
                    Colors InsideColor = colorsCollection.GetColorBySAPId(SAPColorInside);

                    if (InsideColor == null)
                    {
                        var newColor = new Colors
                                           {
                                               IsActive = true,
                                               NameEn = SAPColorInside,
                                               NameRu = SAPColorInside,
                                               NameSAP = SAPColorInside
                                           };

                        result = colorsCollection.Add(newColor, "28prygun01");

                        if (!result)
                            return false;
                    }
                }

                if (!string.IsNullOrEmpty(SAPColorDicht))
                {
                    Colors DictColor = colorsCollection.GetColorBySAPId(SAPColorDicht);

                    if (DictColor == null)
                    {
                        var newColor = new Colors
                                           {
                                               IsActive = true,
                                               NameEn = SAPColorDicht,
                                               NameRu = SAPColorDicht,
                                               NameSAP = SAPColorDicht
                                           };

                        result = colorsCollection.Add(newColor, "28prygun01");

                        if (!result)
                            return false;
                    }
                }
            }


            return result;
        }


        /// <summary>
        /// Синхронизация геометрий
        /// </summary>
        /// <returns>Результат операции</returns>
        public bool GeometriesSyncronise()
        {
            bool result = true;

            var geomCollection = new GeometriesCollection();

            foreach (SapMaterialExport item in DB.Entities.SapMaterialExport.ToList())
            {
                string InstrumentGeom = item.Werkzeuggeo;
                string ProfileGeom = "";
                if (item.Geometrie != null)
                    ProfileGeom = item.Geometrie.ToString();

                if (!string.IsNullOrEmpty(InstrumentGeom))
                {
                    Geometries instrumentGeometry = geomCollection.GetGeometryByName(InstrumentGeom);

                    if (instrumentGeometry == null)
                    {
                        var newGeom = new Geometries { Geometry = InstrumentGeom.ToString(), IsActive = true };

                        result = geomCollection.Add(newGeom, "28prygun01");

                        if (!result)
                            return false;
                    }
                }

                if (!string.IsNullOrEmpty(ProfileGeom))
                {
                    Geometries profileGeometry = geomCollection.GetGeometryByName(ProfileGeom);

                    if (profileGeometry == null)
                    {
                        var newGeom = new Geometries {Geometry = ProfileGeom, IsActive = true};

                        result = geomCollection.Add(newGeom, "28prygun01");

                        if (!result)
                            return false;
                    }
                }
            }


            return result;
        }


        /// <summary>
        /// Синхронизация продуктов
        /// </summary>
        /// <returns>Результат операции</returns>
        public bool ProductsSyncronise()
        {
            bool result = true;

            var col = new ProductsCollection();
            var geomCol = new GeometriesCollection();
            var colorsCollection = new ColorsCollection();
            var pgCol = new ProductGroupsCollection();

            foreach (SapMaterialExport item in DB.Entities.SapMaterialExport.ToList())
            {
                string SAPnumber = item.Материал.ToString();


                int produstId = col.GetIdProductBySAPNumber(SAPnumber);
                if (produstId == 0)
                {
                    var newProduct = new Products();

                    if (SAPnumber.Length == 6)
                        SAPnumber = "00" + SAPnumber;

                    newProduct.SAPMaterialNumber = SAPnumber;

                    double md = 0;
                    if (item.System_Bautiefe != null)
                    {
                        md = Convert.ToDouble(item.System_Bautiefe.ToString());
                    }

                    int mdint;
                    Int32.TryParse(md.ToString(), out mdint);
                    newProduct.MountingDepth = mdint;

                    int pgId = pgCol.GetIdByName(item.Produktgruppe);

                    if (pgId != 0)
                        newProduct.IdProductGroup = pgId;

                    newProduct.Aricle = item.Краткий_текст_материала;

                    newProduct.ArticleLong = item.Краткий_текст_материала;

                    if (!string.IsNullOrEmpty(item.Farbe_Innen))
                    {
                        Colors insideColor = colorsCollection.GetColorBySAPId(item.Farbe_Innen);
                        if (insideColor == null)
                        {
                            ColorsSyncronise();
                        }
                        insideColor = colorsCollection.GetColorBySAPId(item.Farbe_Innen);
                        if ((insideColor == null) && (item.Farbe_Innen != null))
                            return false;
                        newProduct.IdInsideColor = insideColor.IdColor;
                    }


                    if (!string.IsNullOrEmpty(item.Farbe_Aussen))
                    {
                        Colors outsideColor = colorsCollection.GetColorBySAPId(item.Farbe_Aussen);
                        if (outsideColor == null)
                            ColorsSyncronise();
                        outsideColor = colorsCollection.GetColorBySAPId(item.Farbe_Aussen);
                        if (outsideColor != null)
                            newProduct.IdOutsideColor = outsideColor.IdColor;
                        else
                        {
                            return false;
                        }
                    }


                    if (!string.IsNullOrEmpty(item.Dichtungsfarbe))
                    {
                        Colors outsideColor = colorsCollection.GetColorBySAPId(item.Dichtungsfarbe);
                        if (outsideColor == null)
                            ColorsSyncronise();
                        outsideColor = colorsCollection.GetColorBySAPId(item.Dichtungsfarbe);
                        if (outsideColor != null)
                            newProduct.IdPackingColor = outsideColor.IdColor;
                        else
                        {
                            return false;
                        }
                    }

                    Geometries instrumentGeometry, profileGeometry = null, packingGeometry = null;

                    instrumentGeometry = geomCol.GetGeometryByName(item.Werkzeuggeo);
                    packingGeometry = geomCol.GetGeometryByName(item.Dichtungsfarbe);
                    if (item.Geometrie != null) profileGeometry = geomCol.GetGeometryByName(item.Geometrie.ToString());
                    if (profileGeometry == null)
                        GeometriesSyncronise();
                    profileGeometry = geomCol.GetGeometryByName(item.Geometrie.ToString());


                    if (packingGeometry != null)
                        newProduct.IdPackingColor = packingGeometry.IdGeometry;
                    if (instrumentGeometry != null)
                        newProduct.IdInstrumentGeometry = instrumentGeometry.IdGeometry;
                    if (profileGeometry != null)
                        newProduct.IdProfileGeometry = profileGeometry.IdGeometry;


                    double length = 0;

                    if (item.Profillänge != null)
                    {
                        length = Convert.ToDouble(item.Profillänge.ToString());
                    }

                    int lengthInt = 0;

                    Int32.TryParse(length.ToString(), out lengthInt);

                    newProduct.ProfileLength = lengthInt;

                    newProduct.Weight = item.Вес_нетто;

                    if(!string.IsNullOrEmpty(item.System_Name))
                    {
                        var prodSystemsCol = new ProductSystemsCollection();

                        ProductSystems sysName = prodSystemsCol.GetItemByName(item.System_Name) as ProductSystems;

                        if(sysName!=null)
                        {
                            newProduct.IdProductSystem = sysName.IdProductSystem;
                        }
                    }

                    newProduct.IsActive = true;

                    newProduct.IdUser = "28prygun01";

                    result = col.Add(newProduct, "28prygun01");
                    if (!result)
                        return false;
                }
            }

            return result;
        }


        /// <summary>
        /// Синхронизация групп продуктов
        /// </summary>
        /// <returns>Результат операции</returns>
        public bool ProductGroupsSyncronise()
        {
            bool result = true;

            var col = new ProductGroupsCollection();

            foreach (SapMaterialExport item in DB.Entities.SapMaterialExport.ToList())
            {
                if (!string.IsNullOrEmpty(item.Produktgruppe))
                {
                    int pgId = col.GetIdByName(item.Produktgruppe);

                    if (pgId == 0)
                    {
                        var newPg = new ProductGroups
                                        {
                                            DescriptionEn = item.Produktgruppe,
                                            DescriptionRu = item.Produktgruppe,
                                            GroupNameRu = item.Produktgruppe,
                                            GroupNameEn = item.Produktgruppe
                                        };

                        result = col.Add(newPg, "28prygun01");

                        if (!result)
                            return false;
                    }
                }
            }

            return result;
        }
    }
}